package com.topcheer.common.base.util;

import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @author xutao
 * @Package com.topcheer.common.base.util
 * @Description: ${todo}
 * @date 2020/3/31 16:39
 */
public class ELUtil {


    public static String[] getParamterNames(Method method){
        LocalVariableTableParameterNameDiscoverer u =
                new LocalVariableTableParameterNameDiscoverer();
        return  u.getParameterNames(method);
    }

    public static <T> T getKey(String key,Object[] args,String[] paramNames,Class<T> clz){
        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression(key);
        EvaluationContext context = new StandardEvaluationContext();
        for(int i=0;i<args.length;i++){
            context.setVariable(paramNames[i],args[i]);
        }
        return expression.getValue(context,clz);
    }
}
